package jangdolbaeng2.seller.action;

import jangdolbaeng2.seller.dao.BoardDAO;
import jangdolbaeng2.seller.dao.NoticeDAO;
import jangdolbaeng2.seller.dao.ProductManagerDAO;
import jangdolbaeng2.seller.dao.SellerDao;
import jangdolbaeng2.tools.PageNavigator;
import jangdolbaeng2.vo.ProductVO;
import jangdolbaeng2.vo.Qa_commentVO;
import jangdolbaeng2.vo.ShopVO;
import jangdolbaeng2.vo.Shop_blogVO;
import jangdolbaeng2.vo.Shop_noticeVO;
import jangdolbaeng2.vo.Shop_qaVO;

import java.io.BufferedReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.sql.CLOB;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SellerAction extends ActionSupport implements SessionAware{
	
	private SessionMap<String, Object> sessionMap; 	// session
	private List<ProductVO> imageList;				// 상점 상품 이미지 목록
	private Shop_blogVO shop_blog;					// 상점 블로그 정보
	private ShopVO shop;							// 상점 정보
	private List<ShopVO> shopList;					// 상점 목록 정보
	private List<HashMap<String,Object>> blogList;	// 상점 블로그 목록 정보
	private String category;						// 카테고리 
	
	private Shop_qaVO shop_qa;						//게시글 정보
	private int qa_seq;								//게시글 보기, 삭제시 글번호
	private int cmt_seq;							//삭제시 리플 번호
	private int shop_seq;							//가게번호	
	Qa_commentVO reply;
	
	private int spamCheck;							//스팸 체크 여부
	private List<Shop_qaVO> shop_qa_list;			//게시물 목록
	PageNavigator pagenavi;							//페이징 관련 정보
	int currentPage = 1;							//게시물 목록의 현재 페이지
	String searchText;								//검색어
	
	private int insertResult =0; 					// 입력 여부 기본값 0
	private Shop_noticeVO noticeVO;					// 공지사항
	private String notice_title;					// 공지사항 타이틀
	
	private String customer_id;
	
	public String logout(){
		sessionMap.invalidate();
		return SUCCESS;
	}
	
	
	//clob 변환하는 메소드!
		 public String clobToString(CLOB clob) {
			StringBuffer s = new StringBuffer();
	        try {
		        BufferedReader br = new BufferedReader(clob.getCharacterStream());
		        String ts = "";
		        while((ts = br.readLine()) != null) {
		               s.append(ts + "\n");
		        }
		        br.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
	        return s.toString();
		 }	
	
	// blog 리스트
	public String blogList() {
		SellerDao dao = new SellerDao();
		blogList= dao.blogList(category);
		int maxSize= blogList.size();		
		for(int i =0 ;i<maxSize ; i++){
			blogList.get(i).put("BLOG_PHOTO", blogList.get(i).get("BLOG_PHOTO") != null ? clobToString((CLOB) blogList.get(i).get("BLOG_PHOTO")):null);			
		};	
		//System.out.println(blogList);
		return SUCCESS;
	}

	
	// blog 메인 페이지 로드	
	public String blogMain() throws Exception{
		shop_blog=new SellerDao().blogSetup(shop_seq); // 개인 설정 정보를 DB에서 불러옴
		shop=new SellerDao().getShop(shop_seq); // 개인 설정 정보를 DB에서 불러옴		
		ProductManagerDAO pDao= new ProductManagerDAO();
		imageList =pDao.list(shop_seq);
		//System.out.println(imageList);
		sessionMap.put("current_shop_seq", shop_seq);
		return listBoard(); // 리스트 목록 작성
	}
	
	// 금칙어 체크 작성자 : 진승민
	public String spamCheck() {
		BoardDAO dao = new BoardDAO();
		Shop_qaVO spamTest = new Shop_qaVO();
		//shop_seq = 24; // 예제용 shop seq
		// shop_seq= (String) session.get("shop_seq");
		// spamTest에 정보 입력
		spamTest.setShop_seq(shop_seq); 
		spamTest.setQa_title(searchText);
		// dao spam 처리 요청
		spamCheck = dao.spamWords(spamTest);
		
		return SUCCESS;		
	}
	

	//게시판 글 목록 & 공지사항 불러오기	작성자:김기석
	public String listBoard() throws Exception {
		//user.properties에서 지정한 페이징 관련 상수들 읽기
		/*
		int countPerPage = Integer.parseInt(getText("board.countperpage"));		//페이지당 글목록 수
		int pagePerGroup = Integer.parseInt(getText("board.pagepergroup"));		//그룹당 페이지 수
		*/
		int countPerPage = 5;
		int pagePerGroup = 5;
		BoardDAO dao = new BoardDAO();

		//현재 가게 번호 적용
		shop_seq = (int)sessionMap.get("current_shop_seq");
		//전체 글수 구하기
		int total = dao.getTotal(searchText, shop_seq);
		
		
		//PageNavigator 객체 생성 (페이지당 글수, 그룹당 페이지 수, 현재 페이지, 전체 글수)
		pagenavi = new PageNavigator(countPerPage, pagePerGroup, currentPage, total);
		
		//현재 페이지에 해당하는 글 목록 읽기 (전체 레코드 중 보여줄 첫번째 글의 위치, 페이지당 글 수 )
		shop_qa_list = dao.listBoard(searchText, pagenavi.getStartRecord(), pagenavi.getCountPerPage(), shop_seq);
		
		
		System.out.println(sessionMap);
		noticeVO = dao.getNotice(shop_seq);
		//현재 페이지에서의 소비자 확인.
		
		customer_id = (String) sessionMap.get("customer_id");
		System.out.println("customer_id= "+customer_id);
		return SUCCESS;
		
	}
	
	// 공지 글 구하기
	public String noticeGet() throws Exception{
		shop_seq = (int)sessionMap.get("shop_seq");
		NoticeDAO dao = new NoticeDAO();
		noticeVO = dao.noticeGet(shop_seq);
		System.out.println("noticeGet 끝내기 직전 noticeVO = "+noticeVO);
		return SUCCESS;
	}
	
	public String noticeInput() throws Exception{
		System.out.println("액션_NoticeInput 시작");

		shop_seq = (int)sessionMap.get("shop_seq");
		noticeVO = new Shop_noticeVO(shop_seq, notice_title);

		System.out.println("noticeInput 다오 전 noticeVO: "+noticeVO);
		NoticeDAO dao = new NoticeDAO();
		dao.noticeInput(noticeVO);
		System.out.println("공지 입력_액션 완료");
		return SUCCESS;
	}
	
	public String noticeDel() throws Exception{
		System.out.println("액션_noticeDel 시작");
		shop_seq = (int)sessionMap.get("shop_seq");
		new NoticeDAO().noticeDel(shop_seq);
		System.out.println("공지 삭제_액션 완료");
		return SUCCESS;
	}
	
	
	public String getCustomer_id() {
		return customer_id;
	}

	public void setCustomer_id(String customer_id) {
		this.customer_id = customer_id;
	}

	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	public String getNotice_title() {
		return notice_title;
	}

	public void setNotice_title(String notice_title) {
		this.notice_title = notice_title;
	}

	public Shop_noticeVO getNoticeVO() {
		return noticeVO;
	}

	public void setNoticeVO(Shop_noticeVO noticeVO) {
		this.noticeVO = noticeVO;
	}

	public int getQa_seq() {
		return qa_seq;
	}

	public void setQa_seq(int qa_seq) {
		this.qa_seq = qa_seq;
	}

	public int getCmt_seq() {
		return cmt_seq;
	}

	public void setCmt_seq(int cmt_seq) {
		this.cmt_seq = cmt_seq;
	}

	public Qa_commentVO getReply() {
		return reply;
	}

	public void setReply(Qa_commentVO reply) {
		this.reply = reply;
	}

	public int getShop_seq() {
		return shop_seq;
	}

	public void setShop_seq(int shop_seq) {
		this.shop_seq = shop_seq;
	}
	
	public Shop_qaVO getShop_qa() {
		return shop_qa;
	}

	public void setShop_qa(Shop_qaVO shop_qa) {
		this.shop_qa = shop_qa;
	}


	public List<Shop_qaVO> getShop_qa_list() {
		return shop_qa_list;
	}

	public void setShop_qa_list(List<Shop_qaVO> shop_qa_list) {
		this.shop_qa_list = shop_qa_list;
	}

	public PageNavigator getPagenavi() {
		return pagenavi;
	}

	public void setPagenavi(PageNavigator pagenavi) {
		this.pagenavi = pagenavi;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getSearchText() {
		return searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	public int getSpamCheck() {
		return spamCheck;
	}

	public void setSpamCheck(int spamCheck) {
		this.spamCheck = spamCheck;
	}

	public Shop_blogVO getShop_blog() {
		return shop_blog;
	}

	public void setShop_blog(Shop_blogVO shop_blog) {
		this.shop_blog = shop_blog;
	}

	public int getInsertResult() {
		return insertResult;
	}

	public void setInsertResult(int insertResult) {
		this.insertResult = insertResult;
	}	

	public String getCategory() {
		return category;
	}


	public void setCategory(String category) {
		this.category = category;
	}


	public ShopVO getShop() {
		return shop;
	}


	public void setShop(ShopVO shop) {
		this.shop = shop;
	}
	
	public List<HashMap<String, Object>> getBlogList() {
		return blogList;
	}

	public void setBlogList(List<HashMap<String, Object>> blogList) {
		this.blogList = blogList;
	}

	
	
	public List<ProductVO> getImageList() {
		return imageList;
	}



	public void setImageList(List<ProductVO> imageList) {
		this.imageList = imageList;
	}



	public List<ShopVO> getShopList() {
		return shopList;
	}



	public void setShopList(List<ShopVO> shopList) {
		this.shopList = shopList;
	}



	@Override
	public void setSession(Map<String, Object> arg0) {
		 sessionMap =(SessionMap<String, Object>) arg0;	
	}
	
}
